package com.springcloud.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

/**
 * @author xiaolei
 * @version V1.0.1
 * @projectName scums
 * @packageName com.springcloud.service
 * @description desc
 * @email xiaolei@cscongheng.com
 * @date 2020-02-17 16:39
 **/
@Service
public class PayService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private final int totalNum = 100000;

    /**
     * @param num
     * @return
     * @throws Exception
     * @DESC 创建 支付订单
     */
//    @Backoff 重试等待策略
//    delay:指定延迟后重试
//    multiplier:指定延迟的倍数，比如delay=5000l,multiplier=2时，第一次重试为5秒后，第二次为10秒，第三次为20秒
    @Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000L, multiplier = 2))
    public int createOrder(int num) throws Exception {
        logger.info("调用支付接口" + LocalTime.now());

        logger.info("调用支付接口中");
        if (num <= 0) {
            throw new IllegalArgumentException("调用支付接口error");
        }
        logger.info("调用支付接口success"+ LocalTime.now());

        return totalNum - num;
    }

    @Recover
    public int recover(Exception e) {
        logger.info(" ---------------------------  ");
        logger.info(e.getMessage());
        logger.info("记录数据库中");

        return  20;
    }
}
